package com.fz.us.oa.web.controller.mp;

import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.company.bean.EnumManage;
import com.fz.us.admin.company.entity.Admin;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.AdminService;
import com.fz.us.admin.company.service.AuthCodeService;
import com.fz.us.admin.company.service.UserService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 控制器 - 登录
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-29 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/mp/user")
public class UserController extends BaseController {


    private static final long serialVersionUID = 8775152230311420551L;

    @Resource
    UserService userService;
    @Resource
    AdminService adminService;
    @Resource
    AuthCodeService authCodeService;

    @RequestMapping(value = "experience", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String experience(@CurrentUser User user, Model model,String openId) {
        model.addAttribute("openId", openId);
        return "/mp/user_experience";
    }

    /**
     * 成员登录账号校验
     */
    @RequestMapping(value = "validate/account", method = RequestMethod.POST)
    @ResponseBody
    public String validate_account(@CurrentUser User user,
                                   String username) {

        Result result = userService.validateAccount(username);

        return String.valueOf(result.getState() == 1);
    }

    @RequestMapping(value = "resetinput", method = RequestMethod.GET)
    public String resetinput(@CurrentUser User user, Model model, String openId) {
        model.addAttribute("openId", openId);
        return "/mp/resetinput";
    }
    @RequestMapping(value = "sendAuthCode", method = RequestMethod.POST)
    public Object sendAuthCode(@CurrentUser User user, Model model, String mobile){
        System.out.println("发送短信....>>>");
        Result result = userService.sendAuthCode(mobile, EnumManage.AuthCodeEnum.passwordForget,1,null);
        return ajaxJson(result);
    }
    @RequestMapping(value = "updatePass", method = RequestMethod.POST)
    public Object updatePass(@CurrentUser User user, Model model, String mobile, String confirmPassword, String newPassword, String authcode){

        if (!confirmPassword.equals(newPassword)){
           return ajaxJson(resultService.fail("两次输入的密码不一致！"));
        }

        if (StringUtils.isEmpty(newPassword)){
            return ajaxJson(resultService.fail("密码不能为空！"));
        }
        Admin admin = adminService.getAdminByAccount(mobile);
        if(!authCodeService.isValid(admin.getUsermobile(), authcode)){
            return ajaxJson(resultService.fail("验证码错误！"));
        }

        try{
            adminService.ResetPass(admin, newPassword);
        }catch (Exception e){
            return ajaxJson(resultService.fail("密码重置失败！"));
        }
        return ajaxJson(resultService.success("密码重置成功！"));

    }

    /**
     * 新建操作
     */
    @RequestMapping(value = "register/userRegister", method = RequestMethod.POST)
    public Object create(@CurrentUser User user,
                         String username,
                         String password,
                         String name) {
        Company company = adminService.getCompanyByAdminType();
        Result result = userService.createMember(username, password, name, company);

        return ajaxJson(result);
    }
    /**
     * 注册成功
     */
    @RequestMapping(value = "connectMp", method = RequestMethod.POST)
    public Object connectMp(){
        return ajaxJson(resultService.success("绑定成功"));
    }
}
